package xjtu.se.articlecommunity.common.SSE;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xjtu.se.articlecommunity.common.CacheService;
import xjtu.se.articlecommunity.common.CommonService;
import xjtu.se.articlecommunity.common.Result;
import xjtu.se.articlecommunity.common.SSE.service.SSEService;
import xjtu.se.articlecommunity.common.SSE.service.MessService;
import xjtu.se.articlecommunity.common.TokenUtils;
import xjtu.se.articlecommunity.common.dto.CurrentUser;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Random;

@RestController
@RequestMapping("/message")
public class MessAPI {

    @Resource
    private CommonService commonService;

    @Resource
    private CacheService cacheService;

    @Resource
    private SSEService sseService;

    @Resource
    private MessService messService;

    @RequestMapping("/getU_id")
    public Result getU_id(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        return Result.success((Object) currentUser.getU_id());
    }

    @RequestMapping("/receiveMessage/{u_id}")
    public SseEmitter receiveMessage(@PathVariable String u_id) {
        SseEmitter sseEmitter = new SseEmitter(3600_000L);
        cacheService.saveMessSseEmitter(u_id, sseEmitter);
        sseEmitter.onTimeout(() -> cacheService.clearMessSseEmitter(u_id));
        sseEmitter.onCompletion(() -> System.out.println("完成！！！"));
        return sseEmitter;
    }

    @RequestMapping("/getApproveList")
    public Result getApproveList(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(messService.getApproveList(currentUser.getU_id()));
    }

    @RequestMapping("/getLikesList")
    public Result getLikesList(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(messService.getLikesList(currentUser.getU_id()));
    }

    @RequestMapping("/getCommentList")
    public Result getCommentList(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(messService.getCommentList(currentUser.getU_id()));
    }

    @RequestMapping("/getReplyList")
    public Result getReplyList(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(messService.getReplyList(currentUser.getU_id()));
    }
    @RequestMapping("/getNewMessageNumber")
    public Result getNewMessageNumber(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(messService.getNewMessageNumber(currentUser.getU_id()));
    }

    @RequestMapping("/clear/{messType1}/{messType2}")
    public Result clear(@PathVariable String messType1 , @PathVariable String messType2, @RequestHeader("Token") String token) throws IOException {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        messService.clear(messType1, messType2, currentUser.getU_id());
        sseService.push(currentUser.getU_id(), messType1 + "/" + messType2 );

        return Result.success();
    }


    @RequestMapping("/push/{u_id}")
    public Result push(@PathVariable String u_id) throws IOException {
        sseService.push(u_id, ""+Math.random());
        return Result.success();
    }

    @RequestMapping("/test")
    public Result test() throws IOException {
//        messService.sendMessage(MessageType.approve, null, "1001");
//        messService.sendMessage(MessageType.disapprove, null, "1002");
//        messService.sendMessage(MessageType.comment, "1002", "1001");
//        return Result.success(messService.getApproveList("1001"));
        messService.sendMessage(MessageType.comment_like, "1002", "1002");
        return Result.success();
    }
}
